package com.web.music.portal.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.web.music.interfaces.SecurityController;
import com.web.music.model.User;

public class LoginFormController extends SimpleFormController {
	
	private SecurityController securityController;

	public LoginFormController() {
		setCommandClass(User.class);
		setSessionForm(true);
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request) {
		User user = new User();
		return user;
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object object, BindException errors)
			throws Exception {
		ModelAndView model = new ModelAndView(getSuccessView());
		User user = (User) object;
		boolean success = securityController.doLogin(request, user.getUserName(), user.getPassword());
		return model;
	}

	public SecurityController getSecurityController() {
		return securityController;
	}

	public void setSecurityController(
			SecurityController securityController) {
		this.securityController = securityController;
	}

}
